package weixin.popular.api;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;

import weixin.popular.bean.BaseResult;
import weixin.popular.bean.RecordList;
import weixin.popular.bean.SessionParam;
import weixin.popular.bean.KfAccountEntity;
import weixin.popular.bean.KfList;
import weixin.popular.bean.OnlineKfList;
import weixin.popular.bean.SessionStatus;
import weixin.popular.bean.SessionStatusList;
import weixin.popular.bean.WaitCaseResponseEntity;
import weixin.popular.client.LocalHttpClient;

import com.alibaba.fastjson.JSON;

public class CustomServiceAPI extends BaseAPI {

	public static KfList getKfAccountList(String access_token) {
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri("https://api.weixin.qq.com/cgi-bin/customservice/getkflist")
				.addParameter("access_token", access_token).build();
		return LocalHttpClient.executeJsonResult(httpUriRequest, KfList.class);
	}
	
	public static OnlineKfList getOnlineKfList(String access_token) {
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri("https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist")
				.addParameter("access_token", access_token).build();
		return LocalHttpClient.executeJsonResult(httpUriRequest, OnlineKfList.class);
	}
	
	public static BaseResult addKfAccountEntity(String access_token, KfAccountEntity kfAccountEntity) {
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri("https://api.weixin.qq.com/customservice/kfaccount/add")
				.addParameter("access_token", access_token)
				.setEntity(new StringEntity(JSON.toJSONString(kfAccountEntity), "utf-8"))
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class);
	}
	
	public static BaseResult updateKfAccountEntity(String access_token, KfAccountEntity kfAccountEntity) {
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri("https://api.weixin.qq.com/customservice/kfaccount/update")
				.addParameter("access_token", access_token)
				.setEntity(new StringEntity(JSON.toJSONString(kfAccountEntity), "utf-8"))
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class);
	}
	
	public static BaseResult uploadHeadImg(String access_token, String kf_account, File jpg) {
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri("https://api.weixin.qq.com/customservice/kfaccount/update")
				.addParameter("access_token", access_token)
				.addParameter("kf_account", kf_account)
				.setEntity(new FileEntity(jpg))
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class);
	}
	
	public static BaseResult deleteKfAccount(String access_token, String kf_account) {
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri("https://api.weixin.qq.com/customservice/kfaccount/del")
				.addParameter("access_token", access_token)
				.addParameter("kf_account", kf_account)
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class);
	}
	
	public static BaseResult createSession(String access_token, SessionParam sessionParam) {
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri("https://api.weixin.qq.com/customservice/kfsession/create")
				.addParameter("access_token", access_token)
				.setEntity(new StringEntity(JSON.toJSONString(sessionParam), "utf-8"))
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class);
	}
	
	public static BaseResult closeSession(String access_token, SessionParam sessionParam) {
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri("https://api.weixin.qq.com/customservice/kfsession/close")
				.addParameter("access_token", access_token)
				.setEntity(new StringEntity(JSON.toJSONString(sessionParam), "utf-8"))
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class);
	}
	
	public static SessionStatus getSession(String access_token, String openId) {
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri("https://api.weixin.qq.com/customservice/kfsession/close")
				.addParameter("access_token", access_token)
				.addParameter("openid", openId)
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest, SessionStatus.class);
	}
	
	public static SessionStatusList getSessionList(String access_token, String kf_account) {
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri(" https://api.weixin.qq.com/customservice/kfsession/getsessionlist")
				.addParameter("access_token", access_token)
				.addParameter("openid", kf_account)
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest, SessionStatusList.class);
	}
	
	public static WaitCaseResponseEntity getWaitCase(String access_token) {
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri(" https://api.weixin.qq.com/customservice/kfsession/getsessionlist")
				.addParameter("access_token", access_token)
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest, WaitCaseResponseEntity.class);
	}
	
	public static RecordList getRecords(String access_token, Date starttime, Date endtime, int pageindex, int pagesize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("starttime", starttime.getTime() / 1000);
		params.put("endtime", endtime.getTime() / 1000);
		params.put("pageindex", pageindex);
		params.put("pagesize", pagesize);
		HttpUriRequest httpUriRequest = RequestBuilder.post()
				.setHeader(jsonHeader)
				.setUri("https://api.weixin.qq.com/customservice/msgrecord/getrecord")
				.addParameter("access_token", access_token)
				.setEntity(new StringEntity(JSON.toJSONString(params), "utf-8"))
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest, RecordList.class);
	}
}
